package com.pzh.assignment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzh.assignment.common.bean.Result;
import com.pzh.assignment.common.bean.param.MajorParam;
import com.pzh.assignment.common.bean.vo.MajorVO;
import com.pzh.assignment.common.shiro.ShiroUtils;
import com.pzh.assignment.entity.Groups;
import com.pzh.assignment.entity.Major;
import com.pzh.assignment.entity.Role;
import com.pzh.assignment.entity.User;
import com.pzh.assignment.service.IGroupsService;
import com.pzh.assignment.service.IMajorService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pengzhihao
 * @since 2022-03-22
 */
@RestController
@RequestMapping("/major")
public class MajorController {

    @Autowired
    IMajorService majorService;
    @Autowired
    IGroupsService groupsService;

    @RequiresRoles(value = {"sys_admin", "org_admin"}, logical = Logical.OR)
    @GetMapping
    public Result listMajorByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                   @RequestParam(defaultValue = "5") Integer size) {
        Page<Major> page = new Page<>(currentPage, size);
        QueryWrapper<Major> majorQueryWrapper = initMajorWrapper();
        IPage pageData = majorService.page(page, majorQueryWrapper);
        List<MajorVO> voList = ((IPage<Major>) pageData).getRecords().stream().map(MajorVO::valueOf).collect(Collectors.toList());
        pageData.setRecords(voList);
        return Result.success(pageData);
    }

    @GetMapping("/total")
    public Result totalMajor() {
        QueryWrapper<Major> majorQueryWrapper = initMajorWrapper();
        List<Major> majors = majorService.list(majorQueryWrapper);
        List<MajorVO> voList = majors.stream().map(MajorVO::valueOf).collect(Collectors.toList());
        return Result.success(voList);
    }

    @RequiresRoles(value = {"sys_admin", "org_admin"}, logical = Logical.OR)
    @PostMapping
    public Result saveOrUpdate(@RequestBody MajorParam param) {
        Major major = new Major();
        major.setName(param.getName());
        major.setCollegeId(param.getCollegeId());
        if (param.getId() != null) {
            major.setId(param.getId());
        }
        majorService.saveOrUpdate(major);
        return Result.success(null);
    }

    @RequiresRoles(value = {"sys_admin", "org_admin"}, logical = Logical.OR)
    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        majorService.removeById(id);
        return Result.success(null);
    }

    private QueryWrapper<Major> initMajorWrapper(){
        Role role = ShiroUtils.getProfile().getRole();
        Integer groupId = ShiroUtils.getProfile().getGroupId();
        QueryWrapper<Major> wrapper = new QueryWrapper<>();
        switch (role.getName()) {
            case Role.SYSTEM_ADMIN:{
                wrapper.orderByDesc("create_time");
                break;
            }
            default:{
                Groups group = groupsService.getById(groupId);
                wrapper.eq("college_id", group.getCollegeId()).orderByDesc("create_time");
                break;
            }
        }
        return wrapper;
    }
}
